package 结构型模式.享元模式;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 主要角色
 * Flyweight（抽象享元类）：定义享元对象的接口
 * ConcreteFlyweight（具体享元类）：实现抽象享元接口，包含内部状态
 * UnsharedConcreteFlyweight（非共享具体享元类）：不需要共享的享元实现
 * FlyweightFactory（享元工厂类）：创建和管理享元对象
 * Client（客户端）：维持对享元对象的引用，计算或存储外部状态
 */

interface ITree
{
	void render(Integer x,Integer y,Integer z);
}

class Tree implements ITree
{
	private String name;
	private String color;
	private String texture;

	Tree(String name, String color, String texture) {
		this.name = name;
		this.color = color;
		this.texture = texture;
	}
	
	@Override
	public void render(Integer x, Integer y, Integer z) 
	{
		System.out.println("x:"+x+" y:"+y+" z:"+z+"种了一棵"+name+" hash:"+this.hashCode());
	}
}

class TreeFactory
{
	static Map<String,Tree> map = new HashMap();
	
	static Tree getTree(String name, String color, String texture)
	{
		String n = name+color+texture;
		Tree tree = map.get(n);
		if(tree == null)
		{
			Tree t = new Tree(name,color,texture);
			map.put(n,t);
			return map.get(n);
		}
		return tree; 
	}
}

public class Flyweight 
{
	public static void main(String[] args) 
	{
		TreeFactory.getTree("红木","红色","线条").render(10,50,60);
		TreeFactory.getTree("绿木","绿色","线条").render(20,20,60);
		TreeFactory.getTree("绿木","绿色","线条").render(30,30,60);
		TreeFactory.getTree("红木","红色","线条").render(30,10,20);
		TreeFactory.getTree("红木1","红色","线条").render(20,20,20);
	}
}
